// pages/city/city.js
var historical = [];
Page({
  data: {
    hotCities:[],
    cities:[],
  },
  onLoad: async function (options) {
    var module = await import("../../utils/http");
    var cityRes = await module.GetPosition.getCity();
    this.handleHttpData(cityRes);
  },
  onShow(){
    var city = wx.getStorageSync('city');
    if(historical.includes(city)){
      var index = historical.indexOf(city);
      var del = historical.splice(index,1);
      historical.unshift(...del);
    }else if(city){
      historical.unshift(city);
    }
    if(historical.length>4){
      historical.pop()
    }
    this.setData({historical})
  },
  changeCountry(event){
    var country = event.currentTarget.dataset.country;
    wx.setStorageSync('city', country);
    wx.navigateBack();
  },
  handleHttpData(res){
    var hotCities = [];
    res.data.data.hotCities.forEach(item=>{
      hotCities.push(item);
    })
    this.setData({hotCities,cities:res.data.data.cities});
  },
})